from django.shortcuts import render,redirect,HttpResponse
from .models import Category,Link
# Create your views here.


def index(request):
    link_list = Link.objects.all()
    return render(request,'index.html',{"link_list":link_list})



def show(request):
    data = []

    category_list = Category.objects.all()
    # print(category_list)

    for i in category_list:
        item_list = {}

        link_list = Link.objects.filter(category__name=i)
        # print(link_list)


        for link in link_list:
            item_list["category"] = link.category.name

        item_list["falink"] = []
        for link in link_list:
            item = {}
            item["title"] = link.name
            item["link"] = link.slink
            item_list["falink"].append(item)

        print("item_list---*****----", item_list)
        data.append(item_list)
    # print(data)

    return render(request,'show.html',{"data":data})




'''
{
    "category":"Vue",
    "falink":[
        {
            "title":"vue-router",
            "link":"https://router.vuejs.org/zh/"
        },
        {
            "title":"vuex",
            "link":"https://vuex.vuejs.org/"
        },
        {
            "title":"axios",
            "link":"http://www.axios-js.com/"
        }
    ]
}

'''